package com.engineering.elink.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.BlogCo;
import com.engineering.elink.common.Pagination;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.dao.BlogDcMapper;
import com.engineering.elink.dao.BlogMapper;
import com.engineering.elink.dao.BrowseRecordMapper;
import com.engineering.elink.dao.CommentMapper;
import com.engineering.elink.dao.CompanyDynamicMapper;
import com.engineering.elink.dao.DetailCategoryMapper;
import com.engineering.elink.dao.PersonDynamicMapper;
import com.engineering.elink.domain.Blog;
import com.engineering.elink.domain.BlogDc;
import com.engineering.elink.domain.BrowseRecord;
import com.engineering.elink.domain.CompanyDynamic;
import com.engineering.elink.domain.PersonDynamic;
import com.engineering.elink.service.BlogService;
import com.engineering.elink.util.MapConvert;
import com.engineering.elink.util.tool.UUIDUtil;

@Service
public class BlogServiceImpl implements BlogService {
	@Autowired
	private BlogMapper blogMapper;
	@Autowired
	private CommentMapper commentMapper;
	@Autowired
	private DetailCategoryMapper detailCategoryMapper;
	@Autowired
	private BlogDcMapper blogDcMapper;
	@Autowired
	private BrowseRecordMapper browseRecordMapper;
	@Autowired
	private CompanyDynamicMapper companyDynamicMapper;
	@Autowired
	private PersonDynamicMapper  personDynamicMapper;

	@Override
	public Map<String, Object> getList(BlogCo co,String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", blogMapper.selectList(co,userId));
		map.put("Pagination", new Pagination(co, blogMapper.selectCount(co,userId)));
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getBlogListByUserId(BaseCo co, String userId,String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", blogMapper.selectBlogListByUserId(co, userId, type));
		map.put("Pagination",new Pagination(co, blogMapper.selectBlogListCountByUserId(co,userId, type)));
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getInfo(String blogId, String userId, int type) {
		// 1,获取详情
		Blog blog = blogMapper.getInfo(blogId, userId,type);
		if (blog != null) {
			// 2,更新浏览记录
			blog.setBrowseCount(blog.getBrowseCount() + 1);
			blogMapper.updateByPrimaryKeySelective(blog);
			// 3,往浏览记录表俩插入一条数据
			if (userId != null) {
				BrowseRecord br = new BrowseRecord();
				br.setFkId(blogId);
				br.setTime(new Date());
				br.setUserId(userId);
				br.setType(1);
				browseRecordMapper.insertSelective(br);
			}

			// 根据上传时间查询出上一篇和下一篇
			Blog fpre = blogMapper.selectPre(blog.getCreateTime(), type,userId);
			Blog fnext = blogMapper.selectNext(blog.getCreateTime(), type,userId);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("blog", blog);
			map.put("fpre", fpre);
			map.put("fnext", fnext);
			return MapConvert.getMap(ResultType.SUCCESS, map);
		}
		return MapConvert.getMap(ResultType.SUCCESS);
	}

	@Override
	public Map<String, Object> getInfoByUserId(String blogId, String userId,
			int type) {
		// 1,获取详情
		Blog blog = blogMapper.getInfoByUserId(blogId, userId,type);
		if (blog != null) {
			// 2,更新浏览记录
			blog.setBrowseCount(blog.getBrowseCount() + 1);
			blogMapper.updateByPrimaryKeySelective(blog);
			// 3,往浏览记录表俩插入一条数据
			BrowseRecord br = new BrowseRecord();
			br.setFkId(blogId);
			br.setTime(new Date());
			br.setUserId(userId);
			br.setType(1);
			browseRecordMapper.insertSelective(br);

			// 根据上传时间查询出上一篇和下一篇
			Blog fpre = blogMapper.selectPre(blog.getCreateTime(), type,userId);
			Blog fnext = blogMapper.selectNext(blog.getCreateTime(), type,userId);

			Map<String, Object> map = new HashMap<String, Object>();
			map.put("blog", blog);
			map.put("fpre", fpre);
			map.put("fnext", fnext);
			return MapConvert.getMap(ResultType.SUCCESS, map);
		} else {
			return MapConvert.getMap(ResultType.AUTHORITY_ERROR);
		}
	}

	@Override
	public Map<String, Object> add(Blog blog, String dcIds) {
		String blogId = UUIDUtil.getUUID();
		blog.setBlogId(blogId);
		blog.setCreateTime(new Date());
		blogMapper.insertSelective(blog);
		
		//往动态表里插入数据
		if(blog.getType() == 0){//个人博文
			PersonDynamic pd = new PersonDynamic();
			pd.setUserId(blog.getUserId());
			pd.setFkId(blogId);
//			pd.setPdType(1);
			pd.setCreateTime(new Date());
			pd.setPdTitle(blog.getTitle());
			pd.setPdContent(blog.getIntroduction());
			personDynamicMapper.insertSelective(pd);
			
		}else if(blog.getType() == 1){//企业博文
			CompanyDynamic  cd = new CompanyDynamic();
			cd.setCdType(2);
			cd.setCdContent(blog.getTitle());
			cd.setFkId(blogId);
			cd.setCreateTime(new Date());
			cd.setUserId(blog.getUserId());
			companyDynamicMapper.insertSelective(cd);
		}
		// 插入详情分类
		List<BlogDc> fdc = new ArrayList<BlogDc>();
		if (dcIds != "") {
			for (String dcId : dcIds.split(",")) {
				BlogDc bd = new BlogDc();
				bd.setBlogId(blogId);
				bd.setDcId(Integer.parseInt(dcId));
				fdc.add(bd);
			}
			blogDcMapper.insertBatch(fdc);
		}

		return MapConvert.getMap(ResultType.SUCCESS);
	}

	@Override
	public Map<String, Object> update(Blog blog, String dcIds, String oldDcids) {
		Blog oldBlog = blogMapper.selectByPrimaryKey(blog.getBlogId());
		if (oldBlog != null) {
			// 1,根据旧的详情分类ids删除
			blogDcMapper.del(oldDcids);
			// 2,插入详情分类
			List<BlogDc> fdc = new ArrayList<BlogDc>();
			if (dcIds != "") {
				for (String dcId : dcIds.split(",")) {
					BlogDc bd = new BlogDc();
					bd.setBlogId(blog.getBlogId());
					bd.setDcId(Integer.parseInt(dcId));
					fdc.add(bd);
				}
				blogDcMapper.insertBatch(fdc);
			}
			// 3,更新博文
			blog.setUpdateTime(new Date());
			blogMapper.updateByPrimaryKeySelective(blog);
			
			//4.往企业动态表里更新数据
			if(oldBlog.getType() == 2){//企业博文
				CompanyDynamic  cd = new CompanyDynamic();
				cd.setCdType(2);
				cd.setCdContent(blog.getTitle());
				cd.setFkId(oldBlog.getBlogId());
				cd.setCreateTime(new Date());
				cd.setUserId(blog.getUserId());
				companyDynamicMapper.updateByPrimaryKeySelective(cd);
			}

			return MapConvert.getMap(ResultType.SUCCESS);
		} else {
			return MapConvert.getMap(ResultType.BLOG_ERROR);
		}
	}

	@Override
	public Map<String, Object> del(String blogId) {
		//1.查找
		Blog blog = blogMapper.selectByPrimaryKey(blogId);
		//2先删除附表
		 blogDcMapper.del(blogId);
		//3.删除主表
		blogMapper.deleteByPrimaryKey(blogId);
		//4.删除企业动态表里的数据
		if(blog.getType() == 2){//企业博文
			companyDynamicMapper.deleteByFkId(blogId);
		}
		return MapConvert.getMap(ResultType.SUCCESS);
	}

	@Override
	public Map<String, Object> getGreetBlog(int count) {
		return MapConvert.getMap(ResultType.SUCCESS,blogMapper.getGreetBlog(count));
	}

	@Override
	public Map<String, Object> getOtherBlog(BaseCo co, String userId,
			String blogId, String type) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list",blogMapper.selecOtherBlogtList(co, userId, blogId, type));
		map.put("Pagination",new Pagination(co, blogMapper.selectOtherBlogCount(userId,blogId, type)));

		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getOtherBlogByUserId(BaseCo co, String userId,String blogId, String type) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", blogMapper.selecOtherBlogtListByUserId(co, userId,	blogId, type));
		map.put("Pagination",new Pagination(co, blogMapper.selectOtherBlogCountByUserId(userId, blogId, type)));

		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> getMark(String userId, String fkId, int type,int count) {
		return MapConvert.getMap(ResultType.SUCCESS,browseRecordMapper.getMark(userId, fkId, type, count));
	}

	@Override
	public Map<String, Object> updateStationWorks(String blogIds, String userId) {
		return MapConvert.getMap(ResultType.SUCCESS,blogMapper.updateStationWorks(blogIds, userId));
	}

	@Override
	public Map<String, Object> delStationWorks(String blogIds, String userId) {
		return MapConvert.getMap(ResultType.SUCCESS,blogMapper.delStationWorks(blogIds, userId));
	}
}
